// JavaScript Document
var httpRequestObj;
var News_XML
var Scrollbar_Needed = false;

function getNews(s, category, page) {

	var url;
	switch(category)
	{
    case 1: url = "http://news.de.msn.com/rss_newshome.aspx";
            break;
    
    case 2: url = "http://unterhaltung.de.msn.com/rss/rss_star_news.aspx";
            break;
    
    case 3: url = "http://unterhaltung.de.msn.com/rss/rss_entertainment_news.aspx";
            break;
    
    case 4: url = "http://auto.de.msn.com/rss_autonews.aspx";
            break;
  }

	if (window.ActiveXObject) {
		httpRequestObj = new ActiveXObject("Microsoft.XMLHTTP")
		//httpRequestObj = new ActiveXObject("Msxml2.XMLHTTP")
	}
	else {
		var httpRequestObj = new XMLHttpRequest();
    if (typeof netscape != 'undefined' && typeof netscape.security !=
    'undefined') {
    try {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
      httpRequest.open('GET', url, true);
      httpRequest.onreadystatechange = function (evt) {
        if (httpRequest.readyState == 4) {
          
        }
      };
      httpRequest.send(null);
    }
    catch (e) {
      //alert("hilfehilfe");
    }
    }

	}

	httpRequestObj.open("GET", url, true);
	httpRequestObj.onreadystatechange= function () {

		if (httpRequestObj.readyState == 4) {
			News_XML=httpRequestObj.responseXML
			//alert(News_XML);
			if(s) {
				populateSilverlightNews(s, page);
			}
		}
	}

	httpRequestObj.setRequestHeader('Accept','message/x-formresult')
	httpRequestObj.send(null)
	return false
}

function populateSilverlightNews(s, page) {

	
	if(navigator.appName!="Microsoft Internet Explorer") {
		//weak Firefox, strip out white space
		stripWhiteSpace(News_XML);
	}
		
	var stories=News_XML.getElementsByTagName('item');
	
	rssCon=s.findName("NewsItems");
	
	for (i=0;i<stories.length;i++) {		
		
		
		title_val=stories[i].childNodes[0].firstChild.nodeValue;	
		desc = stories[i].getElementsByTagName('description');
		if(desc.length == 0)desc_val = "";
		else desc_val = desc[0].firstChild.nodeValue;
		
		re=/&/g;
		title_val=title_val.replace(re," &amp;");
		desc_val=desc_val.replace(re," &amp;");
		
    re=/\"/g;
    title_val=title_val.replace(re,"'");
    desc_val=desc_val.replace(re,"'");
		
	
		if(stories[i].getElementsByTagName("media:content").length > 0) {
			img_val=stories[i].getElementsByTagName("media:content")[0].attributes.getNamedItem("url").value.split("?")[0];
		}
		else {
			img_val="";
		}
		
		var	news_story_str = '<TextBlock Name="News'+i+'" Canvas.Left="6" Canvas.Top="'+i*17+'" Text="'+title_val+'" TextWrapping="Wrap" FontSize="9" Foreground="#FF1F2146"';
    if(i == 0) news_story_str += ' FontWeight="Bold"';
    news_story_str += ' Cursor="Hand" />';
    
    var news_story_preview = '<Canvas Width="300" Height="100" Name="Preview'+i+'" Background="#FF979797" Opacity="0" Visibility="Collapsed">';
    news_story_preview += '<TextBlock Width="300" Height="100" Text="'+desc_val+'" TextWrapping="Wrap" ';
    news_story_preview += 'FontSize="9" Foreground="#FF1F2146" Cursor="Arrow" />';
    news_story_preview += '</Canvas>'

      var control = s.getHost();
			storyTags = control.content.createFromXaml(news_story_str);
			storyPreview = control.content.createFromXaml(news_story_preview);
			storyTags = trim(s, storyTags);
			
			if(storyTags["Canvas.Top"] < s.findName("NewsItems").Height) {
          rssCon.children.add(storyTags);
          rssCon.children.add(storyPreview);
          storyTags.addEventListener("MouseEnter", Silverlight.createDelegate(page, page.handleMouseIn_News));
          storyTags.addEventListener("MouseLeave", Silverlight.createDelegate(page, page.handleMouseOut_News));
        }
			else{Scrollbar_Needed = true};
		
  	}
}

function stripWhiteSpace(node) {

	nodesToDelete = Array();
	findWhiteSpace(node, 0);

	for(i=nodesToDelete.length-1;i>=0;i--) {
		nodeRef = nodesToDelete[i];
		nodeRef.parentNode.removeChild(nodeRef);
	}
}

function clearNews(s) {
  var canvas = s.findName("NewsItems");
  canvas.Children.Clear();
}

function trim(s, obj) {
    var can_width = Math.round(s.findName("NewsItems").ActualWidth) - Math.round(obj["Canvas.Left"]);
    while(obj.ActualWidth > can_width) {
      obj.Text = obj.Text.substr(0,obj.Text.lastIndexOf(' ')) + "...";
    }
    return obj;
}

function fillTooltip(obj) {
  
}